<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

class HelpDeskControllerAddons extends HelpDeskController 
{
	/**
	 * constructor
	 */	
	function __construct() 
	{
		parent::__construct();
		
		// register tasks
		$this->registerTask( 'list', 'display' );
		$this->registerTask( 'close', 'cancel' );
		$this->registerTask( 'add', 'edit' );
		$this->registerTask( 'new', 'edit' );
		$this->registerTask( 'uninstall', 'delete' );
		$this->registerTask( 'unpublish', 'publish' );
				
	}
	
	/**
	 * display 
	 * @return void
	 */
	function display() 
	{
		HelpDeskRequest::setVar( 'view', 'addons' );
		HelpDeskRequest::setVar( 'layout', 'default'  );
		parent::display();
	}
	
	/**
	 * display the ticket
	 * @return void
	 */
	function view() 
	{
		HelpDeskRequest::setVar( 'view', 'addons' );
		HelpDeskRequest::setVar( 'layout', 'view'  );
		parent::display();
	}
	
	/**
	 * display the edit form
	 * @return void
	 */
	function edit() 
	{
		HelpDeskRequest::setVar( 'view', 'addons' );
		HelpDeskRequest::setVar( 'layout', 'form' );
		parent::display();
	}
	
	/**
	 * cancel and redirect to main page
	 * @return void
	 */
	function cancel() 
	{
		$link = 'index2.php?option='._THISCOMPONENT.'&controller=addons&task=list';
		
		$task = HelpDeskRequest::getVar( 'task' );
		switch (strtolower($task))
		{
			case "cancel":
				$msg = HelpDeskText::_( 'Operation Cancelled' );
				$type = "notice";
			  break;
			case "close":
			default:
				$msg = "";
				$type = "";				
			  break;
		}
	    
	    $this->setRedirect( $link, $msg, $type );		
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	function save() 
	{
		$model 	= &$this->getModel( 'addons' );
		$user 	= HelpDeskConfig::getUser();
		
		$msg = new stdClass();
		$msg->type 		= '';
		$msg->message 	= '';
		$msg->link 		= 'index2.php?option='._THISCOMPONENT.'&controller=addons&task=list';
				
		if ( $data = $model->save() ) 
		{
			$msg->type 		= 'message';
			$msg->message = HelpDeskText::_( 'Save OK' )." - ".$model->getError();
			
			// fire plugins
			$dispatcher = HelpDeskHelperPlugin::getInstance();
			$dispatcher->trigger( 'onAfterSaveSuccessAddons', array( $data, $user, $msg ) );

		} else {
			// save failed
			$msg->type = 'notice';			
			$msg->message = HelpDeskText::_( 'Save Failed' )." - ".$model->getError();
		}

		$this->setRedirect( $msg->link, $msg->message, $msg->type );
		return;
	}
	
	/*
	 * remove record(s)
	 */
	function delete()
	{
		$msg = new stdClass();
		$msg->type 		= '';
		$msg->message 	= '';
		$msg->link 		= 'index2.php?option='._THISCOMPONENT.'&controller=addons&task=list';
				
		$model = $this->getModel('addons');
		
		if (!$model->delete( $msg ))
		{
			$msg->message = HelpDeskText::_('One or More Item Could not be Deleted') . " - " . $msg->message;
		}
		else
		{
			$msg->message = HelpDeskText::_('Items Deleted');
		}
		
		$this->setRedirect( $msg->link, $msg->message, $msg->type );
	}
	
	/*
	 * remove record(s)
	 */
	function publish()
	{
		$msg = new stdClass();
		$msg->type 		= '';
		$msg->message 	= '';
		$msg->link 		= 'index2.php?option='._THISCOMPONENT.'&controller=addons&task=list';
		
		$model = $this->getModel('addons');
		
		if (!$model->publish( $msg ))
		{
			$msg->message = HelpDeskText::_('One or More Item Could not be Un-Published') . " - " . $msg->message;
		}
		else
		{
			$msg->message = HelpDeskText::_('Items Published Status Changed');
		}
				
		$this->setRedirect( $msg->link, $msg->message, $msg->type );
	}

}